package cn.tedu.demo.service.impl;

import cn.tedu.demo.common.Result;
import cn.tedu.demo.entity.User;
import cn.tedu.demo.mapper.UserMapper;
import cn.tedu.demo.service.UserService;
import cn.tedu.demo.vo.PageInfo;
import cn.tedu.demo.vo.SessionStorage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;



    @Override
    @Transactional
    public void save(User user) {
        if (user.getPassword()==null){
            user.setPassword("admin");
            user.setRole(2);
        }
        userMapper.insert(user);
    }

    @Override
    public PageInfo getByPage(PageInfo pageInfo) {
        IPage<User> page=new Page<>(pageInfo.getPageNum(),pageInfo.getPageSize());
        Boolean flag= StringUtils.hasLength(pageInfo.getQuery());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like(flag,"nick_name",pageInfo.getQuery());
        page=userMapper.selectPage(page,userQueryWrapper);
        pageInfo.setTotal(page.getTotal());
        pageInfo.setRows(page.getRecords());
        return pageInfo;
    }

    @Override
    @Transactional
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public SessionStorage login(User user) {
        String password = user.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        SessionStorage sessionStorage = new SessionStorage();
        User user1=userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, user.getUsername()).eq(User::getPassword,user.getPassword()));
        if (user1==null){
            return sessionStorage.setToken("");
        }
        String token= UUID.randomUUID().toString()
                    .replace("-", "");

        sessionStorage.setToken(token)
                      .setUser(user1)  ;
        return sessionStorage;
    }

    @Override
    @Transactional
    public Result<?> register(User user) {
        User user1 = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (user1!=null){
            return Result.error("-1", "用户已经存在请重新输入");
        }
        String password = user.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        user.setRole(2);
        userMapper.insert(user);
        return Result.success();
    }

    @Override
    @Transactional
    public void personUpdate(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User checkUser(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public List<User> count() {
        List<User> userList = userMapper.selectList(null);
        return userList;
    }
}
